package com.welcome_2.beta;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {
	public static final String KEY_ROWID = "id";
	public static final String KEY_USERNAME = "username";
	public static final String KEY_PHONE_NUMBER = "phone_number";
	public static final String KEY_MAC_ADDRESS = "mac_address";
	public static final String KEY_SIGN_UP_DATE = "sign_up_date";
	public static final String KEY_ACCOUNT_ACTIVATION = "account_activation";
	public static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "welcome-2_database";
	private static final String DATABASE_TABLE = "user_accounts";
	private static final int DATABASE_VERSION = 1;
	
	private static final String DATABASE_CREATE = 
			"create table if not exists user_accounts (id integer primary key autoincrement, "
			+ "username VARCHAR(255), phone_number VARCHAR(11), mac_address VARCHAR(255), "
			+ "sign_up_date DATETIME, account_permissions ENUM('a','b','c'), "
			+"account_activation ENUM('0','1'));";

	private final Context context;
	
	static DatabaseHelper DBHelper;
	static SQLiteDatabase db;
	
	public DBAdapter(Context context){
		this.context = context;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper{
		DatabaseHelper(Context context){
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			try{
				db.execSQL(DATABASE_CREATE);
			}catch(SQLException e){
				e.printStackTrace();
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version" + oldVersion + "to" 
					+ newVersion + " , which will destroy all old data");
			
			/**TODO CHANGE THIS! Currently, this deletes information in table. What should be done is
			move the data to another table then copy over
			**/
			db.execSQL("DROP TABLE IF EXISTS user_accounts");
			onCreate(db);
		}
		
		/**Opens the Database**/
		public DatabaseHelper open() throws SQLException{
			DBHelper.getWritableDatabase();
			return this;
		}
		
		/**Closes the Database**/
		public void close(){
			DBHelper.close();
		}
		
		/**Insert record into database**/
		public long insertRecord(String username, String phone_number, String mac_address){
			ContentValues initialValues = new ContentValues();
			initialValues.put(KEY_USERNAME, username);
			initialValues.put(KEY_PHONE_NUMBER, phone_number);
			initialValues.put(KEY_MAC_ADDRESS, mac_address);
			return db.insert(DATABASE_TABLE, null, initialValues);
		}
		
		/**Deletes a particular record**/
		public boolean deleteContact(long rowId){
			return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) >0;
		}
		
		/**Retrieves all records**/
		public Cursor getAllRecords(){
			return db.query(DATABASE_TABLE,  new String[] {KEY_ROWID,  KEY_USERNAME, KEY_PHONE_NUMBER, 
					KEY_MAC_ADDRESS, KEY_SIGN_UP_DATE, KEY_ACCOUNT_ACTIVATION},KEY_ROWID + "=" + null, 
					null, null, null, null);
		}
		
		/**Retrieves a particular record*/
		public Cursor getRecord(long rowId) throws SQLException{
			Cursor mCursor = 
					db.query(true,  DATABASE_TABLE, new String[] {KEY_ROWID,  KEY_USERNAME, KEY_PHONE_NUMBER, 
							KEY_MAC_ADDRESS, KEY_SIGN_UP_DATE, KEY_ACCOUNT_ACTIVATION}, KEY_ROWID + "=" + rowId , 
							null, null, null, null, null);
			if (mCursor != null){
				mCursor.moveToFirst();
			}
			return mCursor;
		}
		
		
		/**Updates a record**/
		public boolean updateRecord(long rowId, String username){
			ContentValues args = new ContentValues();
			args.put(KEY_USERNAME, username);
			return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
		}
	}
}

